﻿using MessagePack;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using XUCore.Configs;
using XUCore.Ddd.Domain;
using XUCore.Excel;
using XUCore.Extensions;
using XUCore.Helpers;
using XUCore.NetCore;
using XUCore.NetCore.HttpFactory;
using XUCore.NetCore.Redis;
using XUCore.Paging;
using XUCore.Serializer;
using XUCore.Webs;

namespace XUCore.ConsoleTests
{
    [MessagePackObject]
    public class User
    {
        [Key(0)]
        public string Id { get; set; }
        [Key(1)]
        public string Name { get; set; }
        [Key(2)]
        public Dictionary<string, object> Dict { get; set; }
        [Key(3)]
        public Info Info { get; set; }
        [Key(4)]
        public List<Info> Infos { get; set; }
        [Key(5)]
        public DateTime CreateTime { get; set; }
    }

    [MessagePackObject]
    public class Info
    {
        [Key(0)]
        public string Picture { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var request = Bson.Node
                .Add("b", Bson.Node.Add("a", 2));

            var doc = Bson.Node
                .Add("a", 1)
                .Add("b", Bson.Node.Add("a", 1))
                .Add("c", new List<Bson>() {
                    Bson.Node
                        .Add("c2",2)
                })
                .Add("d", 2);

            doc.Merge(request);

            doc["b", "a", "a1"] = 1;
            doc["b", "a", "a2"] = 2;
            doc["b", "a", "a3"] = 3;

            return;

            // var jj1 = o1.Get<DateTime>("e");
            // var jj = o1.Get<List<Bson>>("d");

            // var docs = Bson.Nodes;
            // docs.Add(doc);
            // docs.Add(doc);

            // var o2 = docs.ToJson();

            // var o3 = o2.LoadBsons();

            //while (true)
            //{
            //    var number = Console.ReadLine().ToInt();

            //    var html = PageHtml.Build(number, 20, 7, true, new PagerLanguage("首页", "上一页", "下一页", "尾页"), c => $"#{c}");

            //    Console.WriteLine(html);
            //}

            // var o = Str.CreateOrderNumber("O", "yyyyMMdd", 4, isAppendAntifakeEndCode: true);

            // var an = Str.CreateRandomAntifakeCode("N", DateTime.Now, 32, true, true);

            // var dd = Str.AppendAntifakeEndCode("ABC");

            // var ddd = Str.VerifyAntifakeCode(dd);

            // var n = Str.CreateSerialNumber("NO", "");
            // Console.WriteLine(n);
            // n = Str.CreateSerialNumber("NO", n);
            // Console.WriteLine(n);
            // n = Str.CreateSerialNumber("NO", n);
            // Console.WriteLine(n);
            // n = Str.CreateSerialNumber("NO", n);
            // Console.WriteLine(n);
            // n = Str.CreateSerialNumber("NO", n);
            // Console.WriteLine(n);
            // n = Str.CreateSerialNumber("NO", n);
            // Console.WriteLine(n);
            // n = Str.CreateSerialNumber("NO", n);
            // Console.WriteLine(n);
            // n = Str.CreateSerialNumber("NO", n);
            // Console.WriteLine(n);
            // n = Str.CreateSerialNumber("NO", n);
            // Console.WriteLine(n);

            {
                "<Bill>/api/add?id=1&b=2&c=123".AsUrl().ToString();

                "[AppSettings:Bill]/api/add".AsUrl().ToString();

                var s = "[AppSettings:Bill]/api/add".AsUrl()
                    .Add("a", 1)
                    .Post(new { a = 1 })
                    .ResultAsync().Result;
                {
                    using var excelReader = new ExcelReader(@"C:\Users\Nigel\Downloads\结算-出港结算明细_导出任务_20210917_11783720.xlsx");
                    using var sheetReader = excelReader[0];

                    sheetReader.ReadNextInRow(1, 63, out int rowCount, (index, row) =>
                    {

                    });
                }
                {
                    using var excelReader = new ExcelReader(@"C:\Users\Nigel\Downloads\结算-出港结算明细_导出任务_20210917_11783720 - 副本.xlsx");
                    using var sheetReader = excelReader[0];

                    for (var ndx = 1; ndx <= sheetReader.MaxRow; ndx++)
                    {
                        var row = sheetReader.Row(ndx).ToArray();
                    }
                }

                Console.WriteLine("done");
                Console.Read();

                User user = new User
                {
                    Id = "1",
                    Name = "张三1111",
                    CreateTime = DateTime.Now
                };
                user.Dict = new Dictionary<string, object>();
                user.Dict.Add("Id", 1);
                user.Dict.Add("Name", "张三");
                user.Info = new Info { Picture = "picture" };
                user.Infos = new List<Info> {
                    new Info { Picture = "info1" },
                    new Info { Picture = "info2" },
                    new Info { Picture = "info3" },
                    new Info { Picture = "info4" },
                    new Info { Picture = "info5" }
                };

                {


                    var configuration = ConfigHelper.GetJsonConfig("appsettings.Test.json");

                    IRedisService redisService = new RedisServiceProvider(configuration, null);

                    //string hashId = "User";

                    //redisService.HashDelete(hashId, user.Id);

                    //redisService.HashSet<User>(hashId, user.Id, user);

                    //var res = redisService.HashGet<User>(hashId, user.Id);

                    string hashId2 = "User2";

                    redisService.HashDelete(hashId2, "2");

                    redisService.HashSet(hashId2, "2", "2");

                    var res1 = redisService.HashGet<string>(hashId2, "2");

                    string hashId3 = "User3";

                    redisService.HashDelete(hashId3, "3");

                    redisService.HashSet(hashId3, "3", "3");

                    var res3 = redisService.HashGet<string>(hashId3, "3");


                    Console.Read();



                    redisService.StringSet<long>("test", 10);
                }
                Console.ReadKey();
            }
        }
    }
}
